using B.S.XZYData.Domain.XZY;
using B.S.XZYData.Domain.Inventory;
using SqlSugar;
using System;
using System.Collections.Generic; // Added missing import
using System.Linq; // Added missing import

namespace B.S.XZYData.Infrastructure
{
    /// <summary>
    /// 测试数据初始化器
    /// </summary>
    public class TestDataInitializer
    {
        private readonly MyDbContext _dbContext;

        public TestDataInitializer(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 初始化不良品项分布测试数据
        /// </summary>
        public void InitializeDefectiveItemDistributionData()
        {
            try
            {
                // 检查是否已有数据
                var existingCount = _dbContext.Queryable<DefectiveItemDistribution>().Count();
                if (existingCount > 0)
                {
                    Console.WriteLine($"不良品项分布表已有 {existingCount} 条数据，清空后重新初始化");
                    _dbContext.Deleteable<DefectiveItemDistribution>().ExecuteCommand();
                }

                // 创建测试数据
                var testData = new List<DefectiveItemDistribution>
                {
                    new DefectiveItemDistribution
                    {
                        Time = DateTime.Now.AddDays(-5),
                        DefectiveItemCode = "DEF001",
                        DefectiveItemName = "表面划痕",
                        Quantity = 15,
                        TotalQuantity = 1000,
                        Percentage = 1.5m,
                        CreatorId = 1,
                        CreateTime = DateTime.Now,
                        IsDeleted = false
                    },
                    new DefectiveItemDistribution
                    {
                        Time = DateTime.Now.AddDays(-4),
                        DefectiveItemCode = "DEF002",
                        DefectiveItemName = "尺寸偏差",
                        Quantity = 8,
                        TotalQuantity = 800,
                        Percentage = 1.0m,
                        CreatorId = 1,
                        CreateTime = DateTime.Now,
                        IsDeleted = false
                    },
                    new DefectiveItemDistribution
                    {
                        Time = DateTime.Now.AddDays(-3),
                        DefectiveItemCode = "DEF003",
                        DefectiveItemName = "焊接不良",
                        Quantity = 12,
                        TotalQuantity = 600,
                        Percentage = 2.0m,
                        CreatorId = 1,
                        CreateTime = DateTime.Now,
                        IsDeleted = false
                    },
                    new DefectiveItemDistribution
                    {
                        Time = DateTime.Now.AddDays(-2),
                        DefectiveItemCode = "DEF004",
                        DefectiveItemName = "颜色不均",
                        Quantity = 20,
                        TotalQuantity = 1200,
                        Percentage = 1.67m,
                        CreatorId = 1,
                        CreateTime = DateTime.Now,
                        IsDeleted = false
                    },
                    new DefectiveItemDistribution
                    {
                        Time = DateTime.Now.AddDays(-1),
                        DefectiveItemCode = "DEF005",
                        DefectiveItemName = "装配错误",
                        Quantity = 6,
                        TotalQuantity = 400,
                        Percentage = 1.5m,
                        CreatorId = 1,
                        CreateTime = DateTime.Now,
                        IsDeleted = false
                    },
                    new DefectiveItemDistribution
                    {
                        Time = DateTime.Now,
                        DefectiveItemCode = "DEF006",
                        DefectiveItemName = "材料缺陷",
                        Quantity = 10,
                        TotalQuantity = 500,
                        Percentage = 2.0m,
                        CreatorId = 1,
                        CreateTime = DateTime.Now,
                        IsDeleted = false
                    }
                };

                // 插入数据
                _dbContext.Insertable(testData).ExecuteCommand();
                Console.WriteLine($"成功初始化 {testData.Count} 条不良品项分布测试数据");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"初始化不良品项分布测试数据失败: {ex.Message}");
            }
        }



        /// <summary>
        /// 初始化班组和班组成员测试数据
        /// </summary>
        public void InitializeTeamData()
        {
            try
            {
                // 检查班组表是否已有数据
                var existingTeamCount = _dbContext.Queryable<Team>().Count();
                if (existingTeamCount > 0)
                {
                    Console.WriteLine($"班组表已有 {existingTeamCount} 条数据，跳过班组初始化");
                    return;
                }

                // 检查班组成员表是否已有数据
                var existingTeamMemberCount = _dbContext.Queryable<TeamMember>().Count();
                if (existingTeamMemberCount > 0)
                {
                    Console.WriteLine($"班组成员表已有 {existingTeamMemberCount} 条数据，跳过班组成员初始化");
                    return;
                }

                // 创建班组测试数据
                var teams = new List<Team>
                {
                    new Team
                    {
                        TeamCode = "BZ202211260001",
                        TeamName = "注塑班组",
                        TeamType = TeamTypeEnum.InjectionMolding,
                        Remark = "测试班组",
                        CreateTime = DateTime.Now.AddDays(-5),
                        CreatorId = 1,
                        IsDeleted = false
                    },
                    new Team
                    {
                        TeamCode = "BZ202211260002",
                        TeamName = "仓库班组1",
                        TeamType = TeamTypeEnum.Warehouse,
                        Remark = "测试班组",
                        CreateTime = DateTime.Now.AddDays(-4),
                        CreatorId = 1,
                        IsDeleted = false
                    },
                    new Team
                    {
                        TeamCode = "BZ202211270001",
                        TeamName = "组装班组",
                        TeamType = TeamTypeEnum.Assembly,
                        Remark = "1111",
                        CreateTime = DateTime.Now.AddDays(-3),
                        CreatorId = 1,
                        IsDeleted = false
                    },
                    new Team
                    {
                        TeamCode = "BZ202211270002",
                        TeamName = "测试班组",
                        TeamType = TeamTypeEnum.Warehouse,
                        Remark = "1111",
                        CreateTime = DateTime.Now.AddDays(-2),
                        CreatorId = 1,
                        IsDeleted = false
                    },
                    new Team
                    {
                        TeamCode = "BZ202211270003",
                        TeamName = "测试班组2",
                        TeamType = TeamTypeEnum.Warehouse,
                        Remark = "1111",
                        CreateTime = DateTime.Now.AddDays(-1),
                        CreatorId = 1,
                        ModifyTime = DateTime.Now,
                        ModifierId = 1,
                        IsDeleted = false
                    }
                };

                // 插入班组数据
                _dbContext.Insertable(teams).ExecuteCommand();
                Console.WriteLine($"成功初始化 {teams.Count} 条班组测试数据");

                // 创建班组成员测试数据
                var teamMembers = new List<TeamMember>
                {
                    new TeamMember
                    {
                        TeamId = 5, // 测试班组2
                        MemberName = "admin",
                        MemberRealName = "超级管理员",
                        Phone = "13888888888",
                        CreateTime = DateTime.Now,
                        CreatorId = 1,
                        ModifyTime = DateTime.Now,
                        ModifierId = 1,
                        IsDeleted = false
                    },
                    new TeamMember
                    {
                        TeamId = 5, // 测试班组2
                        MemberName = "admin666",
                        MemberRealName = "Admin测试",
                        Phone = "",
                        CreateTime = DateTime.Now,
                        CreatorId = 1,
                        ModifyTime = DateTime.Now,
                        ModifierId = 1,
                        IsDeleted = false
                    },
                    new TeamMember
                    {
                        TeamId = 5, // 测试班组2
                        MemberName = "test",
                        MemberRealName = "测试",
                        Phone = "",
                        CreateTime = DateTime.Now,
                        CreatorId = 1,
                        ModifyTime = DateTime.Now,
                        ModifierId = 1,
                        IsDeleted = false
                    }
                };

                // 插入班组成员数据
                _dbContext.Insertable(teamMembers).ExecuteCommand();
                Console.WriteLine($"成功初始化 {teamMembers.Count} 条班组成员测试数据");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"初始化班组测试数据失败: {ex.Message}");
            }
        }
    }
}
